package com.sunday.common.mq.rabbit.study.spring.study.e4_Configuring_Broker;


import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration
 * https://github.com/openzipkin/brave/blob/master/instrumentation/spring-rabbit/README.md
 */
@Slf4j
@Configuration
public class RabbitConfiguration {

    public static final String EXCHANGE_NAME = "configuring_broker_exchange";
    public static final String ROUTING_KEY_NAME = "configuring_broker_routing_key";
    public static final String QUEUE_NAME = "configuring_broker_queue";

    /**
     * https://docs.spring.io/spring-amqp/docs/current/reference/html/#broker-configuration
     * <p>
     * AMQP 规范描述了如何使用协议在代理上配置队列、交换和绑定。 这些操作（可从 0.8 及更高规范移植）存在于包的接口中。 该类的 RabbitMQ 实现位于包中。AmqpAdminorg.springframework.amqp.coreRabbitAdminorg.springframework.amqp.rabbit.core
     * 该接口基于使用 Spring AMQP 域抽象，如以下清单所示：AmqpAdmin
     */
    @Bean
    public Queue queue() {
//        return new Queue(QUEUE_NAME);
        return QueueBuilder.nonDurable(QUEUE_NAME)
                .autoDelete()
                .exclusive()
                .withArgument("foo", "bar")
                .build();
    }

    @Bean
    public DirectExchange directExchange() {
//        return new DirectExchange(EXCHANGE_NAME);
        return ExchangeBuilder.directExchange(EXCHANGE_NAME)
                .autoDelete()
                .internal()
                .withArgument("foo", "bar")
                .build();
    }


    @Bean
    public Queue allArgs1() {
        return QueueBuilder.nonDurable("all.args.1")
                .ttl(1000)
                .expires(200_000)
                .maxLength(42)
                .maxLengthBytes(10_000)
                .overflow(QueueBuilder.Overflow.rejectPublish)
                .deadLetterExchange("dlx")
                .deadLetterRoutingKey("dlrk")
                .maxPriority(4)
                .lazy()
                .leaderLocator(QueueBuilder.LeaderLocator.minLeaders)
                .singleActiveConsumer()
                .build();
    }

    @Bean
    public DirectExchange ex() {
        return ExchangeBuilder.directExchange("ex.with.alternate")
                .durable(true)
                .alternate("alternate")
                .build();
    }

    @Bean
    public Binding binding() {
//        return new Binding(QUEUE_NAME, Binding.DestinationType.QUEUE, EXCHANGE_NAME, ROUTING_KEY_NAME, null);
        return BindingBuilder.bind(queue())
                .to(directExchange())
                .with(ROUTING_KEY_NAME);

    }

}
